package com.gdth.widget.gridview.adapter;

import java.util.List;

import com.gdth.R;
import com.gdth.model.ImageItem;
import com.gdth.utils.ImageUtil;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * GridView数据源
 * @author Arvin
 *
 */
public class BaseGridViewAdapter extends BaseAdapter{
	
	protected Context mContext;
	
	protected List<ImageItem> vList;
	
	protected int layout = R.layout.aron_gridview_item;
	
	public BaseGridViewAdapter(Context context, List<ImageItem> vList) {  
        this.mContext = context;
        this.vList = vList;
    }
	
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return this.vList.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	@Override
	public View getView(int arg0, View arg1, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ImageItem model = vList.get(arg0);
		
		ViewHolder holder = null;
		if (arg1 == null){
			holder = new ViewHolder();
			
			arg1 = LayoutInflater.from(this.mContext).inflate(layout, arg2, false);
			
		    holder.localView = (TextView) arg1.findViewById(R.id.gridview_item_txt);
	
		    arg1.setTag(holder);
        }
		else {
			holder = (ViewHolder) arg1.getTag();
		}
		
		if (holder != null){
			holder.localView.setText(model.getValue());
			
			if (model.getImg() != null){
				String iname = model.getImg();
				
				//获取图片在Android的ID
				int id = this.mContext.getResources().getIdentifier(iname, "drawable", this.mContext.getPackageName());

				Drawable mDrawable = ImageUtil.getImageAsDrawable(mContext, id);
				mDrawable.setFilterBitmap(true);
				
				holder.localView.setCompoundDrawablesWithIntrinsicBounds(null, mDrawable, null, null);
		    }
		}
		
        return arg1;  
	}
	
	public static class ViewHolder{
		public TextView localView = null;
	}

}
